package cn.nexd.map.rendering.core.helper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.nexd.map.location.collector.bean.BluetoothDataResult;
import cn.nexd.map.location.collector.bean.WifiDataResult;
import cn.nexd.map.rendering.core.signal.WifiSignalDeal;

/**
 * Created by lawrence on 2015/11/14.
 */
public class WifiSignalHelper {


    private static List<List<WifiDataResult>> wifiDateLists = new ArrayList<>();
    private static List<List<BluetoothDataResult>> blueDataLists = new ArrayList<>();
    private static Map<String, WifiSignalDeal> wifiSignalDealMap;
    private static List<WifiDataResult> avgWifiDataResults;
    private static List<BluetoothDataResult> avgBlueDataResults;

    public static List<WifiDataResult> getWeightedAvgWifiDataResult(List<WifiDataResult> wifiDataResults) {
        if (avgWifiDataResults == null) {
            avgWifiDataResults = new ArrayList<>();
        } else {
            avgWifiDataResults.clear();
        }
        if (wifiSignalDealMap == null) {
            wifiSignalDealMap = new HashMap<>();
        }
        if (wifiDateLists.size() >= 5) {
            wifiDateLists.remove(0);
        }
        wifiDateLists.add(wifiDataResults);

        for (List<WifiDataResult> wifiDataResultss : wifiDateLists) {
            for (WifiDataResult wifiDataResult : wifiDataResultss) {
                if (wifiSignalDealMap.containsKey(wifiDataResult.getBssid())) {
                    wifiSignalDealMap.get(wifiDataResult.getBssid()).add(wifiDataResult.getLevel());
                } else {
                    WifiSignalDeal wifiSignalDeal = new WifiSignalDeal();
                    wifiSignalDeal.add(wifiDataResult.getLevel());
                    wifiSignalDealMap.put(wifiDataResult.getBssid(), wifiSignalDeal);
                }
            }
        }
        for (Map.Entry<String, WifiSignalDeal> entry : wifiSignalDealMap.entrySet()) {
            WifiDataResult avgWifiDataResult = new WifiDataResult();
            avgWifiDataResult.setBssid(entry.getKey());
            avgWifiDataResult.setLevel(entry.getValue().getWeightedAvgValue());
            avgWifiDataResults.add(avgWifiDataResult);
        }
        wifiSignalDealMap.clear();
        return avgWifiDataResults;
    }

    public static List<BluetoothDataResult> getAvgBlueDataResult(List<BluetoothDataResult> bluetoothDataResults) {
        if (avgBlueDataResults == null) {
            avgBlueDataResults = new ArrayList<>();
        } else {
            avgBlueDataResults.clear();
        }
        if (wifiSignalDealMap == null) {
            wifiSignalDealMap = new HashMap<>();
        }
        if (blueDataLists.size() >= 3) {
            blueDataLists.remove(0);
        }
        blueDataLists.add(bluetoothDataResults);


        for (List<BluetoothDataResult> wifiDataResultss : blueDataLists) {
            for (BluetoothDataResult wifiDataResult : wifiDataResultss) {
                if (wifiSignalDealMap.containsKey(wifiDataResult.getAddress())) {
                    wifiSignalDealMap.get(wifiDataResult.getAddress()).add(wifiDataResult.getRssi());
                } else {
                    WifiSignalDeal wifiSignalDeal = new WifiSignalDeal();
                    wifiSignalDeal.add(wifiDataResult.getRssi());
                    wifiSignalDealMap.put(wifiDataResult.getAddress(), wifiSignalDeal);
                }
            }
        }

        for (Map.Entry<String, WifiSignalDeal> entry : wifiSignalDealMap.entrySet()) {
            BluetoothDataResult avgWifiDataResult = new BluetoothDataResult();
            avgWifiDataResult.setAddress(entry.getKey());
            avgWifiDataResult.setRssi(entry.getValue().getRecAvgValue());
            avgBlueDataResults.add(avgWifiDataResult);
        }
        wifiSignalDealMap.clear();
        return avgBlueDataResults;
    }

    public static List<WifiDataResult> getAvgWifiDataResult(List<WifiDataResult> wifiDataResults) {
        if (avgWifiDataResults == null) {
            avgWifiDataResults = new ArrayList<>();
        } else {
            avgWifiDataResults.clear();
        }
        if (wifiSignalDealMap == null) {
            wifiSignalDealMap = new HashMap<>();
        }
        if (wifiDateLists.size() >= 3) {
            wifiDateLists.remove(0);
        }
        wifiDateLists.add(wifiDataResults);


        for (List<WifiDataResult> wifiDataResultss : wifiDateLists) {
            for (WifiDataResult wifiDataResult : wifiDataResultss) {
                if (wifiSignalDealMap.containsKey(wifiDataResult.getBssid())) {
                    wifiSignalDealMap.get(wifiDataResult.getBssid()).add(wifiDataResult.getLevel());
                } else {
                    WifiSignalDeal wifiSignalDeal = new WifiSignalDeal();
                    wifiSignalDeal.add(wifiDataResult.getLevel());
                    wifiSignalDealMap.put(wifiDataResult.getBssid(), wifiSignalDeal);
                }
            }
        }

        for (Map.Entry<String, WifiSignalDeal> entry : wifiSignalDealMap.entrySet()) {
            WifiDataResult avgWifiDataResult = new WifiDataResult();
            avgWifiDataResult.setBssid(entry.getKey());
            avgWifiDataResult.setLevel(entry.getValue().getRecAvgValue());
            avgWifiDataResults.add(avgWifiDataResult);
        }
        wifiSignalDealMap.clear();
        return avgWifiDataResults;
    }
}
